﻿//************************************************************************************************
// Copyright © 2003-2012 Steven M. Cohn. All Rights Reserved.
//
//************************************************************************************************

namespace CookieChipper
{
	using System;
	using System.Text;
	using System.Windows.Forms;


	internal partial class PropertiesWindow : OptionalForm
	{
		private const int BlockSize = 128;


		public PropertiesWindow ()
		{
			InitializeComponent();
		}


		public PropertiesWindow (CacheHeader header)
			: this()
		{
			CacheLocation location;
			CookieCacheProvider.GetDirectoryPath(out location);
			pathBox.Text = System.IO.Path.GetFullPath(header.Path)
				+ Environment.NewLine
				+ "(" + location.GetDescription() + ")";

			signatureBox.Text = header.Signature;

			fileSizeBox.Text = header.FileSize.ToString("N0");
			fileSizeKBox.Text = FormatBytes(header.FileSize);

			hashLabel.Text = "Hash tables (" + header.HashTables.ToString() + "):";
			hashTablesBox.Text = header.HashBlocks.ToString();
			hashKBox.Text = FormatBytes(header.HashBlocks * BlockSize);

			recordsBox.Text = header.Records.ToString();
			slotsBox.Text = "(" + ((int)((header.HashBlocks * 128) / 8) - header.Records).ToString() + " free)";

			blocksBox.Text = header.TotalBlocks.ToString("N0");
			blocksKBox.Text = FormatBytes(header.TotalBlocks * BlockSize);

			allocatedBox.Text = header.AllocatedBlocks.ToString("N0");
			allocatedKBox.Text = FormatBytes(header.AllocatedBlocks * BlockSize);

			cacheBox.Text = header.CacheSize.ToString("N0");
			cacheKBox.Text = FormatBytes(header.CacheSize);

			quotaBox.Text = header.CacheQuota.ToString("N0");
			quotaKBox.Text = FormatBytes(header.CacheQuota);
		}


		private string FormatBytes (int bytes)
		{
			if (bytes == 0)
			{
				return "(0KB)";
			}
			else if (bytes < 1024)
			{
				return "(<1KB)";
			}

			int kb = (int)(bytes / 1024);
			return "(" + kb.ToString("N0") + "KB)";
		}
	}
}
